import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Service from '../views/Service'
import Stadium from '../views/Stadium.vue'

Vue.use(VueRouter)

VueRouter.prototype.goBack = function () {
  this.isBack = true
  window.history.go(-1)
}

const routes = [
  {
    path: '/succeed_pay',
    name: '支付成功页面',
    component: () => import('../views/Succeed-pay.vue')
  },
  {
    path: '/about_class_sj',
    name: 'About_class_sj',
    component: () => import('../views/About_class_sj.vue')
  },
  {
    path: '/pt_details/:zid?',
    props: true,
    name: 'pt_details',
    component: () => import('../components/Pt_details.vue')
  },
  {
    path: '/succeed',
    name: 'Succeed',
    component: () => import('../views/Succeed.vue')
  },
  {
    path: '/about_class/:zid?',
    props: true,
    name: 'About_class',
    component: () => import('../views/About_class.vue')
  },
  {
    path: '/membership_card_details/:zid?',
    props: true,
    name: '会员卡详情页',
    component: () => import('../views/Membership_card_details.vue')
  },
  {
    path: '/appointment',
    name: '我的-我的预约页面',
    component: () => import('../views/Appointment.vue')
  },
  {
    path: '/privateCourses',
    name: 'PrivateCourses',
    component: () => import('../components/PrivateCourses.vue')
  },
  {
    path: '/group_class_details/:zid?',
    props: true,
    name: 'group_class_details',
    component: () => import('../views/Group_class_details.vue')
  },
  {
    path: '/leisureclass/:active',
    props: true,
    name: 'routeName',
    component: () => import('../views/Leisureclass.vue'),
    meta: {
      HomeHeader: true
    }
  },
  {
    path: '/',
    name: 'home',
    component: Home,
    meta: {
      HomeHeader: true
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },

  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/my',
    name: 'My',
    component: () => import('../views/My.vue')
  },
  {
    path: '/stadium',
    name: '场馆列表',
    component: Stadium,
    meta: {
      HomeHeader: true
    }
  },
  {
    path: '/stadium_details/:vid?',
    props: true,
    name: '场馆详情页',
    component: () => import('../views/Stadium_details.vue')
  },
  {
    path: '/information',
    name: 'Information',
    component: () => import('../views/Information.vue')
  },
  {
    path: '/information_details',
    name: 'Information_details',
    component: () => import('../views/Information_details.vue')
  },
  {
    path: '/service',
    name: 'Service',
    component: Service,
    children: [
      {
        path: 'about_my',
        component: () => import('../components/AboutMy.vue'),
        meta: {
          title: 'about_my'
        }
      },
      {
        path: 'feed_back',
        component: () => import('../components/FeedBack.vue'),
        meta: {
          title: 'feed_back'
        }
      },
      {
        path: 'online',
        component: () => import('../components/Online.vue'),
        meta: {
          title: 'online'
        }
      }
    ]
  },
  {
    path: '/config',
    name: 'Config',
    component: () => import('../views/Config.vue')
  },
  {
    path: '/config_details',
    name: 'ConfigDetails',
    component: () => import('../views/ConfigDetails.vue'),
    children: [
      {
        path: 'account_mag',
        component: () => import('../components/AccountMag.vue')
      },
      {
        path: 'user_agreement',
        component: () => import('../components/UserAgreement.vue')
      },
      {
        path: 'privacy_policy',
        component: () => import('../components/PrivacyPolicy.vue')
      }
    ]
  }
]

const router = new VueRouter({
  // mode: 'history',
  routes
})

// 调用路由实例对象的 beforeEach 方法，挂载路由导航守卫
// to是我们跳转的路径，from是来自的路径，next为放行函数
router.beforeEach((to, from, next) => {
  // 用户访问首页路径
  if (to.path === '/') {
    // 从 localStorage 中获取到保存的token值
    const token = localStorage.getItem('token')
    // 若 token 存在，则放行
    if (token) return next()
    // 若 token 不存在，则跳转到登录页面
    next('/Login')
  } else {
    next()
  }
})

export default router
